package xcmg.device.web.ratingManager;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.ratingManager.CompleteRateConfigService;
import xcmg.device.service.vo.FunctionResult;
import xcmg.device.service.vo.ratingManager.BasisQueryVO;
import xcmg.device.service.vo.ratingManager.CompleteRateConfigVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.Arrays;

/**
 * Created by wrg on 2019/7/10.
 */
@RestController
@RequestMapping("/completeRateConfig")
@Api("总体销售完成率配置入口")
public class CompleteRateConfigController extends BasicController {
    @Autowired
    private CompleteRateConfigService completeRateConfigService;

    @PostMapping(value = "/add")
    @ApiOperation(value = "新增销售完成情况配置")
    @FastMappingInfo(needLogin = true)
    public ActionResult add(@RequestBody CompleteRateConfigVO completeRateConfigVO) throws Exception {
        if (null == completeRateConfigVO) {
            return actionResult(ErrorCode.IllegalArument);
        }

        completeRateConfigVO.setCreatorId(getUserId());
        completeRateConfigVO.setCreatorName(getUserName());
        return actionResult(completeRateConfigService.add(completeRateConfigVO));
    }

    @PostMapping(value = "/delete")
    @ApiOperation(value = "删除销售完成情况配置")
    @FastMappingInfo(needLogin = true)
    public ActionResult remove(@RequestBody String[] ids) throws Exception {
        if (ids == null || ids.length == 0) {
            return actionResult(ErrorCode.IllegalArument);
        }
        try {
            ErrorCode ret = completeRateConfigService.remove(Arrays.asList(ids));
            return actionResult(ret);
        } catch (Exception e) {
            return new ActionResult<>(ErrorCode.Failure.getCode(), e.getMessage(), null);
        }
    }

    @PostMapping(value = "/update")
    @ApiOperation(value = "修改销售完成情况配置")
    @FastMappingInfo(needLogin = true)
    public ActionResult update(@RequestBody CompleteRateConfigVO completeRateConfigVO) throws Exception {
        if (null == completeRateConfigVO) {
            return actionResult(ErrorCode.IllegalArument);
        }
        completeRateConfigVO.setModifierId(getUserId());
        completeRateConfigVO.setModifierName(getUserName());
        return actionResult(completeRateConfigService.update(completeRateConfigVO));
    }

    @PostMapping(value = "/list")
    @ApiOperation(value = "获取销售完成情况配置列表")
    @FastMappingInfo(needLogin = true)
    public ActionResult list(@RequestBody SearchCommonVO<BasisQueryVO> condition) throws Exception {
        if (condition.getFilters() == null) {
            return actionResult(ErrorCode.IllegalArument);
        }

        PageCommonVO pageCommonVO = completeRateConfigService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @GetMapping(value = "/item")
    @ApiOperation(value = "获取销售完成情况配置详情")
    @FastMappingInfo(needLogin = true)
    public ActionResult item(@RequestParam String id) throws Exception {
        if (StringUtils.isBlank(id)) {
            return actionResult(ErrorCode.IllegalArument);
        }
        return actionValueResult(completeRateConfigService.item(id));
    }

    @ResponseBody
    @PostMapping(value = "/import")
    @FastMappingInfo(needLogin = true)
    public ActionResult importExcel(HttpServletRequest request) throws Exception {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartRequest.getFile(multipartRequest.getFileNames().next());
        String fileName = file.getOriginalFilename();
        FunctionResult<String> result = new FunctionResult<>();
        if (!fileName.trim().endsWith(".xls")) {
            result.setCode(ErrorCode.ImportExcelEndWithXLS);
        } else {
            InputStream in = file.getInputStream();
            result = completeRateConfigService.importExcel(in, request.getParameter("x-ratingId"), getUserId(), getUserName(), getOrgId());
        }
        return actionResult(result.getCode(), result.getValue());
    }

    @PostMapping(value = "/download")
    @FastMappingInfo(needLogin = true)
    public void download() throws Exception {
        completeRateConfigService.download();
    }
}
